//
//  LSZStatusRetweetView.swift
//  醒浪微博
//
//  Created by 雷顺哲 on 16/7/13.
//  Copyright © 2016年 LSZ. All rights reserved.
//

import UIKit
import SnapKit
//转发微博视图
class LSZStatusRetweetView: UIView {
    //  记录转发微博底部约束
    var retweetViewBottom: Constraint?

//   持有的viewModel
    var statusViewModel: LSZStatusViewModel? {
        didSet {
//            设置转发微博内容
            contentLabel.text = statusViewModel?.retweetContent
            
//            卸载上一次底部约束
            retweetViewBottom?.uninstall()
            
//            判断是否有转发微博配图
            if let count = statusViewModel?.status?.retweeted_status?.pic_urls?.count where count > 0 {
//                有配图
                
//                设置模型
                pictureView.picUrls = statusViewModel?.status?.retweeted_status?.pic_urls
                pictureView.hidden = false

                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewBottom = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
            }else {
//                没有配图
                pictureView.hidden = true
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewBottom = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
            }
            
            
        }
    }
    
//     MARK: 懒加载控件
    private lazy var contentLabel: UILabel = {
       let label = UILabel(textColor: UIColor.grayColor(), fontSize: 14)
        label.numberOfLines = 0
        return label
    }()
//    转发微博配图
    private lazy var pictureView: LSZStatusPictureView = {
        let collec = LSZStatusPictureView()
//        设置配图的背景色和转发微博的背景色一致
        collec.backgroundColor = self.backgroundColor
        return collec
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        contentLabel.backgroundColor = UIColor(white: 0.95, alpha: 1)
//        添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
//        设置约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(HomeCellMargin)
            make.leading.equalTo(self).offset(HomeCellMargin)
            make.trailing.equalTo(self).offset(HomeCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(HomeCellMargin)
        }
        
//        微博的底部约束
        self.snp_makeConstraints { (make) -> Void in
          self.retweetViewBottom = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
        
        
    }
    
    
}





















